package com.ubs.training.web.controller;

import com.ubs.training.server.dao.model.Country;
import com.ubs.training.server.dao.model.Product;
import com.ubs.training.server.service.ClientService;
import com.ubs.training.server.service.CountryService;
import com.ubs.training.server.service.ProductService;
import com.ubs.training.server.service.TypeService;
import com.ubs.training.web.util.ProductValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/22/12
 * Time: 4:51 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class ProductController {

    @Autowired
    ProductService svp;

    @Autowired
    CountryService svc;

    @Autowired
    TypeService svt;

    @Autowired
    ClientService svcl;




    @RequestMapping(value = "client/{clientId}/product/list.do", method = RequestMethod.GET)
    public String listProduct(@PathVariable("clientId") int clientId, Model model) {
        model.addAttribute("products", svp.getByClientId(clientId));
        model.addAttribute("clientId", clientId);
        model.addAttribute("countries", svc.getAllCountries());

//        List<Country> countryList = svc.getAllCountries();
//        Map<Integer, String> resultsMap = new HashMap<Integer, String>();
//        for (Country o : countryList) {
//            resultsMap.put(o.getID(), (String) o.getCName());
//        }


        model.addAttribute("client",svcl.getById(clientId));
        model.addAttribute("types",svt.getAll());

        return "listproducts";
    }

    @RequestMapping(value = "clients/{clientId}/product/{id_product}/edit.do", method = RequestMethod.GET)
    public String getProduct(@PathVariable("clientId") int clientId,@PathVariable("id_product") int id_product, Model model) {
        Product product;
        if (id_product>0){
            product = svp.getProductById(id_product);
        } else{
            product = new Product();
            Date date = new Date(System.currentTimeMillis());
            product.setStart_date(date);
        }
        model.addAttribute("product", product);
        model.addAttribute("countries", svc.getAllCountries());
        model.addAttribute("types", svt.getAll());
        model.addAttribute("date", System.currentTimeMillis());
        model.addAttribute("clientId", clientId);

        return "editproduct";
    }

    @InitBinder
    void initBind(DataBinder bind) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.setLenient(false);
        bind.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }                                                                                     //  "yyyy-MM-dd' 'HH:mm

    @RequestMapping(value = "clients/{clientId}/product/{id_product}/edit.do", method = RequestMethod.POST)
    public String saveProduct(@PathVariable("clientId") int clientId,@PathVariable("id_product") int id_product,Model model, @ModelAttribute("product") Product product, BindingResult res){
        new ProductValidator(product, res).validate();
        product.setId_product(id_product);
        product.setClientId(clientId);
        if (res.hasErrors()){
            model.addAttribute("countries", svc.getAllCountries());
            model.addAttribute("types", svt.getAll());
            return "editproduct";
        }
        svp.saveProduct(product);
        return "redirect:/client/{clientId}/product/list.do";
    }

    @RequestMapping(value = "client/{clientId}/product/{id_product}/delete.do", method = RequestMethod.GET)
    public String getProduct(@PathVariable("clientId") int clientId,@PathVariable("id_product") int id_product) {
        Product product = new Product();
        product = svp.getProductById(id_product);
        svp.deleteProduct(product);
        return "redirect:/client/{clientId}/product/list.do";
    }
}


